home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-02-02 | 5.0 KB | 150 lines | [TEXT/ttxt] |
- The following is a description and sample code of how I handle user
- The following was submitted by Peter Thomas:
- _____________________________________________________________
-
- The following is a description and sample code of how I handle user
- authentication in my database. Feel free to mod to your requirements.
-
- First, you need to define a basic authentication header which will replace
- the standard 200 header. You can see this header in Webstar's log if you
- switch on realms and try to hit a page in a secure realm.
-
-
- ------
- C_STRING(255;◊vAuthHdr)
- ◊vAuthHdr:="HTTP/1.0 401 Unauthorized"+◊crlf+"Server: WebSTAR/1.1
- "+◊crlf+"MIME-Version: 1.0"+◊crlf+"WWW-Authenticate: Basic realm="
-
-
- --------
-
- Then I have a little routine which outputs the authentication prompt on
- demand. I didn't used to do this, until I got caught by a 'feature' in
- 1.1b4 which won't let you set the HTTP header. So you set it to null, and
- just appendreply the authentication header.
- The old, more logical, code is commented out.
- I encase my realm in @ characters just to make it stand out, as in some
- cases I am putting long messages in there to tell the user what to do, eg
- "enter your credit card number now" which looks a bit funny in the middle
- of a "Enter user name for " .... " on 123.123.123.123" type prompt.
- -------
- ` Procedure PTAuthPr
- ` puts out the http authentication prompt
- ` with the user realm specified by $1
-
- C_STRING(255;$sRealm;$1)
- $sRealm:=$1
-
- `NL_SetHTTPHdr (VReqID;◊vAuthHdr+◊quote+"@"+$sRealm+"@"+◊quote+◊crlf+◊crlf)
- `NL_AppendReply (vReqID;"")
-
- NL_SetHTTPHdr (VReqID;"")
- NL_AppendReply (vReqID;◊vAuthHdr+◊quote+"@"+$sRealm+"@"+◊quote+◊crlf+◊crlf)
- -------
-
- Then I have a routine which allows people to log in to the system. I put a
- field on the form for them to put in their user name as well as put it into
- the authentication prompt. This may look superfluous, but believe me, I
- found it almost impossible to work out where a user was in the input
- process unless I had this independent field to tell me who he wanted to log
- in as. Remember, browsers will remember a user name and password until they
- are forced not to. So If you have multiple users logging in to one machine,
- it can be difficult trying to force the browser to forget the previous user
- and get a new one.
- --------
-
-
-
- $UName:=aFields{$NameIndx}
-
- If (vUser#$UName) ` wants to log in as someone else
- PTAuthPr ("EQUALS")
- Else
-
- SEARCH([User4];[User4]Name=vUser)
- If (Records in selection([User4])#1)
- PTAuthPr ("EQUALS")
- Else
- ` found the user ok, but is the password correct
- If (vPass#[User4]Password)
- PTAuthPr ("Incorrect password")
- Else
- [User4]LoggedIn:=True
- [User4]LoginDate:=Current date
- [User4]LoginTime:=Current time
- SAVE RECORD([User4])
- Case of
- : ([User4]Type=◊Student)
- EqStudent
- : ([User4]Type=◊Agent)
- EqAgent
- : ([User4]Type=◊Institution)
- EqInst
- End case
- UNLOAD RECORD([User4])
- End if
- End if
- End if
-
- -------
- Then for every procedure which can be called from outside via the browser,
- I have an authorisation check which determines whether the user is logged
- in, and then whether they can validly access this particular function.
- -------
-
-
- ` Procedure PTAuthCheck
- ` checks the current user name and password against the function and button
- ` (if any exists) which they are trying to invoke
- ` This routine outputs any error messages or prompts required
-
- C_STRING(64;$System;$Function;$Action)
-
- $System:=$1 ` the current system name
- $Function:=$2 ` the name of the function being accessed
- $Action:=$3 ` the name of the button being actioned
- $0:=◊NOK ` Nobody gets past unless specifically authorised!
-
- If (vUser="")
- vErrMsg:="Sorry, no user name, please log in first."
- NL_AppendReply (vReqID;vErrMsg)
- Else
- READ WRITE([User4])
- SEARCH([User4];[User4]Name=vUser)
- If (Records in selection([User4])#1)
- vErrMsg:="Sorry, invalid user name, please log in."
- NL_AppendReply (vReqID;vErrMsg)
- Else
- If (vPass#[User4]Password)
- vErrMsg:="Your password is not correct"
- NL_AppendReply (vReqID;vErrMsg)
- Else
- If (Not([User4]LoggedIn))
- vErrMsg:="Please log in first."
- NL_AppendReply (vReqID;vErrMsg)
- Else
- If ([User4]LoginTime<(Current time-3600))
- [User4]LoggedIn:=False
- SAVE RECORD([User4])
- vErrMsg:="Please log in again."
- NL_AppendReply (vReqID;vErrMsg)
- Else
- [User4]LoggedIn:=True
- [User4]LoginDate:=Current date
- [User4]LoginTime:=Current time
- vUserID:=[User4]ID
- SAVE RECORD([User4])
- Case of
- : ([User4]Type=◊Student)
- etc, etc, checking for each function type, name, action, etc
-
- ----
-
- Note, I don't do any communication with Webstar and it's user names and
- passwords. All my pages are served from 4D, so there is no need, although
- it can be done.
-
- Cheers
- Peter Thomas
- pthomas@spirit.com.au
-